package org.example.hbase2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;

public class HBaseDML {
    private static final Logger logger = LoggerFactory.getLogger(HBaseDML.class);

    private static Configuration conf;
    private static Connection con;
    private static Admin admin;

    public static void init() throws IOException {
        // conf
        conf = HBaseConfiguration.create();
        logger.info("dev conf home--->{}", System.getenv("DEV_CONF_HOME"));
        //conf.addResource(new Path(System.getenv("DEV_CONF_HOME"), "core-site.xml"));
        //conf.addResource(new Path(System.getenv("DEV_CONF_HOME"), "hbase-site.xml"));
        conf.set("hbase.zookeeper.quorum", "hadoop1, hadoop2, hadoop3");
        // connector
        User user = new User() {
            @Override
            public String getShortName() {
                return "*";
            }

            @Override
            public <T> T runAs(PrivilegedAction<T> privilegedAction) {
                return null;
            }

            @Override
            public <T> T runAs(PrivilegedExceptionAction<T> privilegedExceptionAction) throws IOException, InterruptedException {
                return null;
            }
        };
        con = ConnectionFactory.createConnection(conf, user);
        // ddl object
        admin = con.getAdmin();
        // operation
    }

    public static void uninit() {
        try {
            admin.close();
            con.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }

    public static void getData(String tn, String rowKey, String cf, String cn) {
        try {
            Table table = con.getTable(TableName.valueOf(tn));
            Get get = new Get(Bytes.toBytes(rowKey));
            // get.addFamily(Bytes.toBytes(cf));
            // get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
            Result result = table.get(get);
            for (Cell cell : result.rawCells()) {
                logger.info("CF-->{}, CN-->{}, value-->{}",
                        Bytes.toString(CellUtil.cloneFamily(cell)),
                        Bytes.toString(CellUtil.cloneQualifier(cell)),
                        Bytes.toString(CellUtil.cloneValue(cell)));
            }

            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

    public static void main(String[] args) throws IOException {
        System.out.println("dev conf home--->{}" + System.getenv("DEV_CONF_HOME"));
        init();
        getData("bigdata:test", "row1", "info", "name");
        uninit();
    }
}
